﻿// -------------------------------------------------------------------------
//    @FileName         :    NFFriendModule.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-03-31
//    @Email			:    445267987@qq.com
//    @Module           :    NFFriendModule
//
// -------------------------------------------------------------------------

#include "NFFriendModule.h"

#include <NFPackageUtil.h>
#include <NFPacketSendUtil.h>
#include <NFPlayer.h>
#include <NFSession.h>
#include "proto_svr_common.nanopb.h"
#include "proto_svr_friend.pb.h"

NFFriendModule::NFFriendModule(NFIPluginManager* p):NFLogicDynamicModule(p)
{
}

NFFriendModule::~NFFriendModule()
{
}

int NFFriendModule::Awake()
{
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_SNS_FRIEND_QUERY_REQ);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_FRIEND_1V1_INVITE_REQ);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_SNS_FRIEND_OPERATE_REQ);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_SNS_FRIEND_RECOMMEND_REQ);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_FRIEND_NOT_RECOMMAND_REQ);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_SNS_QUERY_ROLE_REQ);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_SG_FRIEND_QUERY_RSP);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_SG_FRIEND_OPERATE_RSP);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_SG_FRIEND_RECOMMEND_RSP);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_SG_ROLE_QUERY_RSP);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_SG_FRIEND_1V1_INVITE_RSP);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_CS_FRIEND_QUERY_ROLE_REQ);
    RegisterClientMessage(NF_ST_LOGIC_SERVER, proto_ff::E_SG_FRIEND_ALL_INFO_NTF);
    return 0;
}

int NFFriendModule::OnHandleClientMessage(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);
    if (!m_pObjPluginManager->IsInited(NF_ST_LOGIC_SERVER))
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server not inited, drop client msg:{}", packet.ToString());
        return -1;
    }

    if (m_pObjPluginManager->IsServerStopping())
    {
        NFLogError(NF_LOG_DEFAULT, packet.nParam1, "Logic Server is Stopping, drop client msg:{}", packet.ToString());
        return proto_ff::ERR_CODE_SERVER_IS_STOPPING;
    }

    int iRetCode = 0;
    switch (packet.nMsgId)
    {
        case proto_ff::E_CS_SNS_FRIEND_QUERY_REQ:
        {
            OnHandleFriendQueryReq(packet, pSession, req);
            break;
        }
        default:
            NFLogError(NF_LOG_DEFAULT, 0, "not handle msg:{}", packet.nMsgId);
    }

    return iRetCode;
}

int NFFriendModule::OnHandleFriendQueryReq(NFDataPackage& packet, NFSession* pSession, const proto_ff::Proto_CSReq& req)
{
    CHECK_NULL(0, pSession);
    NFPlayer* pPlayer = pSession->GetPlayer();

    proto_ff::Proto_GSFriendQueryReq gsReq;
    gsReq.set_role_id(pPlayer->GetRoleID());

    pPlayer->SendMsgToSnsServer(proto_ff::E_GS_FRIEND_QUERY_REQ, gsReq);
    return 0;
}
